#import "CustomTabView.h"
#import "Masonry.h"
#import "ImageTopButton.h"
@implementation CustomTabView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        [self setupViews];
    }
    return self;
}

- (void)setupViews {
    // 收藏按钮
    ImageTopButton *favoriteButton = [ImageTopButton buttonWithType:UIButtonTypeCustom];
    [favoriteButton setImage:[UIImage imageNamed:@"fav"] forState:UIControlStateNormal];
    [favoriteButton setTitle:@"收藏" forState:UIControlStateNormal];
    favoriteButton.titleLabel.font = [UIFont systemFontOfSize:14];
    favoriteButton.tag = 4;

    [self addSubview:favoriteButton];
    
    // 聊一聊按钮
    ImageTopButton *chatButton = [ImageTopButton buttonWithType:UIButtonTypeCustom];
    [chatButton setImage:[UIImage imageNamed:@"shareChat"] forState:UIControlStateNormal];
    [chatButton setTitle:@"聊一聊" forState:UIControlStateNormal];
    chatButton.titleLabel.font = [UIFont systemFontOfSize:14];
    [self addSubview:chatButton];
    chatButton.tag = 5;

    // 见面箱按钮
    ImageTopButton *boxButton = [ImageTopButton buttonWithType:UIButtonTypeCustom];
    [boxButton setImage:[UIImage imageNamed:@"email"] forState:UIControlStateNormal];
    [boxButton setTitle:@"见面箱" forState:UIControlStateNormal];
    boxButton.titleLabel.font = [UIFont systemFontOfSize:14];
    [self addSubview:boxButton];
    boxButton.tag = 1;

    // 加入见面箱按钮
    UIButton *addToBoxButton = [UIButton buttonWithType:UIButtonTypeCustom];
    addToBoxButton.layer.borderWidth = 1;
    addToBoxButton.layer.borderColor = [UIColor lightGrayColor].CGColor;
    addToBoxButton.layer.cornerRadius = 18;
    [addToBoxButton setTitle:@"加入见面箱" forState:UIControlStateNormal];
    addToBoxButton.titleLabel.font = [UIFont systemFontOfSize:13];
    [addToBoxButton setTitleColor:kColorHEX(0x333333) forState:UIControlStateNormal];
    [self addSubview:addToBoxButton];
    addToBoxButton.tag = 2;

    // 立即下单按钮
    UIButton *orderButton = [UIButton buttonWithType:UIButtonTypeCustom];
    orderButton.layer.cornerRadius = 15;
    [orderButton setTitle:@"立即下单" forState:UIControlStateNormal];
    orderButton.titleLabel.font = [UIFont systemFontOfSize:13];
    [orderButton setTitleColor:kColorHEX(0x333333) forState:UIControlStateNormal];
    [self addSubview:orderButton];
    orderButton.tag = 3;
    
    [favoriteButton addTarget:self action:@selector(favoriteAction:) forControlEvents:UIControlEventTouchUpInside];
    [chatButton addTarget:self action:@selector(chatAction:) forControlEvents:UIControlEventTouchUpInside];

    
    
    [boxButton addTarget:self action:@selector(goPersonMainPage:) forControlEvents:UIControlEventTouchUpInside];
    [addToBoxButton addTarget:self action:@selector(goPersonMainPage:) forControlEvents:UIControlEventTouchUpInside];
    [orderButton addTarget:self action:@selector(goPersonMainPage:) forControlEvents:UIControlEventTouchUpInside];

    
    CAGradientLayer *gradientLayer = [GradientHelper createGradientWithFrame:CGRectMake(0, 0, 200, 200)
                                                                           startColor:kColorHEX(0xFFD800)
                                                                             endColor:kColorHEX(0xFFE44E)];
    
    
    // 将渐变图层添加到按钮的layer
     [orderButton.layer insertSublayer:gradientLayer atIndex:0];
    
    // 添加布局约束，使渐变图层跟随按钮大小变化
     orderButton.layer.masksToBounds = YES;
     orderButton.layer.cornerRadius = 15.0;
    // 使用Masonry进行布局
    [favoriteButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self).offset(15);
        make.centerY.equalTo(self).offset(-10);
    }];
    
    [chatButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(favoriteButton.mas_right);
        make.centerY.equalTo(self).offset(-10);
    }];
    
    [boxButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(chatButton.mas_right);
        make.centerY.equalTo(self).offset(-10);
    }];
    
    [addToBoxButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(boxButton.mas_right).offset(13);
        make.centerY.equalTo(self).offset(-10);;
        make.width.equalTo(@90);
        make.height.equalTo(@35);
        make.right.lessThanOrEqualTo(orderButton.mas_left).offset(-8);
    }];
    
    [orderButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self).offset(-8);
        make.centerY.equalTo(self).offset(-10);
        make.width.equalTo(@80);
        make.height.equalTo(@35);

    }];
}
- (void)goPersonMainPage:(UIButton *)sedner {
    if (self.delegate && [self.delegate respondsToSelector:@selector(goAddresssAction:)]) {
        [self.delegate goAddresssAction:sedner.tag];
    }
}
- (void)favoriteAction:(UIButton *)sedner {
    if (self.delegate && [self.delegate respondsToSelector:@selector(favoriteClickAction:)]) {
        [self.delegate favoriteClickAction:sedner];
    }
}
- (void)chatAction:(UIButton *)sedner {
    if (self.delegate && [self.delegate respondsToSelector:@selector(chatClickAction:)]) {
        [self.delegate chatClickAction:sedner.tag];
    }
}
@end
